package com.aco.party.web.controller.system;

import com.aco.party.model.po.Links;
import com.aco.party.model.vo.LinksParam;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.LinksService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditLinksCommand;
import com.aco.party.web.validator.system.EditLinksValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;
/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageLinksController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageLinksController.class);

    @Resource
    private LinksService linksService;

    private EditLinksValidator validator = new EditLinksValidator();

    private final String REDIRECTEDIT = "redirect:edit";

    private final String 合作伙伴 = "合作伙伴";
    private final String 友情链接 = "友情链接";
    private final String 活动报道 = "活动报道";

    @RequestMapping(value = {"/links", "/links/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageLinks";
    }

    @RequestMapping(value = "links/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <Links> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <Links> page = new Page<>();
        entry.webParamConvert();
        LinksParam param = new LinksParam();
        param.setType(合作伙伴);
        List <Links> list = linksService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(linksService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/links/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditLinksCommand command, Model model) {
        if (command.getLinkId() != null) {
            Links po = linksService.getByPrimaryKey(command.getLinkId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
            command.setLinkVisible("Y");
        }
        return "system/editLinks";
    }

    @RequestMapping(value = "/links/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Links links = new Links();
                command.toPo(links);
            links.setLinkNotes(合作伙伴);
            linksService.update(links);
            model.addAttribute("no_error",true);
        }
        return "system/editLinks";
    }

    @RequestMapping(value = "/links/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditLinksCommand command) {
        command.setLinkVisible("Y");
        return "system/editLinks";
    }

    @RequestMapping(value = "/links/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editLinks";
        }
        Links links = new Links();
        command.toPo(links);
        links.setLinkNotes(合作伙伴);
        linksService.add(links);
        attributes.addAttribute("linkId",links.getLinkId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/links/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        linksService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){

    }

    /**
     *
     *
     * 友情链接
     *
     * **/

    @RequestMapping(value = {"/friends_links", "/friends_links/list"}, method = RequestMethod.GET)
    public String showFriendsLinksManagePage(HttpServletResponse response) {
        return "system/manageLinks_friends";
    }

    @RequestMapping(value = "/friends_links/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <Links> getFriendsLinksListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <Links> page = new Page<>();
        entry.webParamConvert();
        LinksParam param = new LinksParam();
        param.setType(友情链接);
        List <Links> list = linksService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(linksService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/friends_links/edit", method = RequestMethod.GET)
    public String showFriendsLinksEdit(HttpServletResponse response,@ModelAttribute EditLinksCommand command, Model model) {
        if (command.getLinkId() != null) {
            Links po = linksService.getByPrimaryKey(command.getLinkId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
            command.setLinkVisible("Y");
        }
        return "system/editLinks_friends";
    }

    @RequestMapping(value = "/friends_links/edit", method = RequestMethod.POST)
    public String editFriendsLinks(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Links links = new Links();
            command.toPo(links);
            links.setLinkNotes(友情链接);
            linksService.update(links);
            model.addAttribute("no_error",true);
        }
        return "system/editLinks_friends";
    }

    @RequestMapping(value = "/friends_links/add", method = RequestMethod.GET)
    public String showAddFriendsLinks(@ModelAttribute EditLinksCommand command) {
        command.setLinkVisible("Y");
        return "system/editLinks_friends";
    }

    @RequestMapping(value = "/friends_links/add", method = RequestMethod.POST)
    public String addFriendsLinks(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editLinks_friends";
        }
        Links links = new Links();
        command.toPo(links);
        links.setLinkNotes(友情链接);
        linksService.add(links);
        attributes.addAttribute("linkId",links.getLinkId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/friends_links/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response delFriendsLinks(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        linksService.delete(id);
        return new Response();
    }


    /**
     *
     * 活动相关报道
     *
     *
     */
    @RequestMapping(value = {"/activities_links", "/activities_links/list"}, method = RequestMethod.GET)
    public String showActivitiesLinksManagePage(HttpServletResponse response) {
        return "system/manageLinks_activities";
    }

    @RequestMapping(value = "/activities_links/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <Links> getActivitiesLinksListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <Links> page = new Page<>();
        entry.webParamConvert();
        LinksParam param = new LinksParam();
        param.setType(活动报道);
        List <Links> list = linksService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(linksService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/activities_links/edit", method = RequestMethod.GET)
    public String showActivitiesLinksEdit(HttpServletResponse response,@ModelAttribute EditLinksCommand command, Model model) {
        if (command.getLinkId() != null) {
            Links po = linksService.getByPrimaryKey(command.getLinkId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
            command.setLinkVisible("Y");
        }
        return "system/editLinks_activities";
    }

    @RequestMapping(value = "/activities_links/edit", method = RequestMethod.POST)
    public String editActivitiesLinks(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Links links = new Links();
            command.toPo(links);
            links.setLinkNotes(活动报道);
            linksService.update(links);
            model.addAttribute("no_error",true);
        }
        return "system/editLinks_activities";
    }

    @RequestMapping(value = "/activities_links/add", method = RequestMethod.GET)
    public String showAddActivitiesLinks(@ModelAttribute EditLinksCommand command) {
        command.setLinkVisible("Y");
        return "system/editLinks_activities";
    }

    @RequestMapping(value = "/activities_links/add", method = RequestMethod.POST)
    public String addActivitiesLinks(Model model, @ModelAttribute EditLinksCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editLinks_activities";
        }
        Links links = new Links();
        command.toPo(links);
        links.setLinkNotes(活动报道);
        linksService.add(links);
        attributes.addAttribute("linkId",links.getLinkId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/activities_links/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response delActivitiesLinks(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        linksService.delete(id);
        return new Response();
    }
}
